package com.example.demo.controller;

import com.example.demo.model.Worker;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/worker")
public class WorkerController {

    @PostMapping("/create")
    public ResponseEntity<Worker> createWorker(@RequestBody @Valid Worker worker){
        // if age is less than 18, it will throw an exception
        //2024-08-31T10:01:15.759+08:00  WARN 35997 --- [demo] [nio-8080-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public org.springframework.http.ResponseEntity<com.example.demo.model.Worker> com.example.demo.controller.WorkerController.createWorker(com.example.demo.model.Worker): [Field error in object 'worker' on field 'age': rejected value [17]; codes [Min.worker.age,Min.age,Min.int,Min]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [worker.age,age]; arguments []; default message [age],18]; default message [Age must be greater than or equal to 18]] ]

        return ResponseEntity.ok(worker);
    }
}
